package net.sk.china.city.controller;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sk.china.city.controller.base.BaseController;
import net.sk.china.city.domain.ArTeam;
import net.sk.china.city.domain.SiteTreeNode;
import net.sk.china.city.domain.SysDept;
import net.sk.china.city.domain.dto.AjaxResult;
import net.sk.china.city.domain.dto.TableDataInfo;
import net.sk.china.city.domain.dto.team.TeamInfoVO;
import net.sk.china.city.domain.dto.team.TeamSearchDTO;
import net.sk.china.city.domain.dto.team.TeamUpdateDTO;
import net.sk.china.city.service.IArTeamService;
import net.sk.china.city.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 志愿者队伍Controller
 *
 * @author ruoyi
 * @date 2024-04-07
 */
@Api(tags = "队伍管理")
@RestController
@RequestMapping("/system/team")
public class ArTeamController extends BaseController
{
    @Autowired
    private IArTeamService arTeamService;
    @Autowired
    private ISysDeptService deptService;

    /**
     * 查询志愿者队伍列表
     */
    @ApiOperation("志愿者队伍分页查询")
    @PreAuthorize("hasAuthority('user:team:list')")
    @GetMapping("/page")
    public TableDataInfo<ArTeam> page(TeamSearchDTO teamSearchDTO)
    {
        startPage();
        List<ArTeam> list = arTeamService.selectArTeamList(teamSearchDTO);
        return getDataTable(list);
    }

    @ApiOperation("获取站点树形结构")
    @GetMapping("/site")
    public AjaxResult getSiteTree() {
        List<SiteTreeNode> siteTreeNodeList = deptService.selectSiteTree();
        return success(siteTreeNodeList);
    }

    @ApiOperation("获取站点树形结构2")
    @GetMapping("/site2")
    public AjaxResult getSiteTree2() {
        List<SiteTreeNode> siteTreeNodes = deptService.selectSiteTree2();
        return success(siteTreeNodes);
    }


    @ApiOperation("获取队伍选项列表")
    @GetMapping("/list")
    public AjaxResult list() {
        return success(arTeamService.selectTeamOptionList());
    }

    /**
     * 导出志愿者队伍列表
     */
    @ApiOperation(value = "志愿者队伍导出", hidden = true)
    @PreAuthorize("hasAuthority('user:team:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, TeamSearchDTO teamSearchDTO)
    {
        List<ArTeam> list = arTeamService.selectArTeamList(teamSearchDTO);
//        ExcelUtil<ArTeam> util = new ExcelUtil<ArTeam>(ArTeam.class);
//        util.exportExcel(response, list, "志愿者队伍数据");
    }

    /**
     * 获取志愿者队伍详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:team:query')")
//    @GetMapping(value = "/{pkId}")
//    public AjaxResult getInfo(@PathVariable("pkId") Long pkId)
//    {
//        return success(arTeamService.selectArTeamByPkId(pkId));
//    }

    /**
     * 新增志愿者队伍
     */
    @ApiOperation("新增志愿者队伍")
    @PreAuthorize("hasAuthority('user:team:add')")
    @PostMapping
    public AjaxResult add(@RequestBody TeamUpdateDTO teamUpdateDTO)
    {
        return toAjax(arTeamService.insertArTeam(teamUpdateDTO));
    }

    /**
     * 修改志愿者队伍
     */
    @ApiOperation("修改志愿者队伍")
    @PreAuthorize("hasAuthority('user:team:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody TeamUpdateDTO teamUpdateDTO)
    {
        return toAjax(arTeamService.updateArTeam(teamUpdateDTO));
    }

    /**
     * 删除志愿者队伍
     */
    @ApiOperation("志愿者队伍删除")
    @PreAuthorize("hasAuthority('user:team:remove')")
	@DeleteMapping("/{pkIds}")
    public AjaxResult remove(@PathVariable Long[] pkIds)
    {
        return toAjax(arTeamService.deleteArTeamByPkIds(pkIds));
    }


    @ApiOperation("通过名称查询志愿者队伍")
    @GetMapping("/nameToInfo")
    public AjaxResult getTeam(String teamName) {
        return success(arTeamService.getTeamByTeamName(teamName));
    }

    @ApiOperation("获取站点信息")
    @GetMapping("/getTreeInfo")
    public AjaxResult getTreeInfo() {
        List<TeamInfoVO> infoVOS = new ArrayList<>();

        List<SysDept> sysDeptList = deptService.selectAllDept();

        sysDeptList.forEach(item->{
            if (ObjectUtil.isNotEmpty(item.getParentId())){
                infoVOS.add(new TeamInfoVO(item.getDeptId(),item.getDeptName(),item.getParentId()));
            }
        });

        List<ArTeam> teamList = arTeamService.selectAllTeam();

        teamList.forEach(item->{
            if (ObjectUtil.isNotEmpty(item.getSiteId())){
                TeamInfoVO teamInfoVO = new TeamInfoVO(IdUtil.getSnowflakeNextId(), item.getTeamName(), item.getSiteId());
                System.err.println(teamInfoVO);
                infoVOS.add(new TeamInfoVO(IdUtil.getSnowflakeNextId(),item.getTeamName(),item.getSiteId()));}
        });
        //配置 树
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        //设置ID对应的名称
        treeNodeConfig.setIdKey("pkId");
        //设置节点名对应的名称
        treeNodeConfig.setNameKey("teamName");
        treeNodeConfig.setParentIdKey("parentId");
        // 最大递归深度 4级目录
        treeNodeConfig.setDeep(5);

        //转换器 最顶层的id是0
        List<Tree<String>> treeNodes = TreeUtil.build(infoVOS, "100", treeNodeConfig,
                (treeNode, tree) -> {
                    //ID
                    tree.setId(treeNode.getPkId().toString());
                    //获取父节点ID
                    tree.setParentId(treeNode.getParentId().toString());
                    //名称
                    tree.setName(treeNode.getTeamName());
                });
        return success(treeNodes);
    }

    @ApiOperation("通过id查询名称")
    @GetMapping("/deptName")
    public AjaxResult getTeamName(@RequestParam Long id){
        SysDept sysDept = deptService.selectDeptById(id);
        return success(sysDept.getDeptName());
    }

}
